import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('NormalMonster')
export class NormalMonster extends Component {
    private hp: number = 10;
    private attack: number = 2;
    private speed: number = 70;
    roadIndex = 0;
    nextRoadNode:Node = null;
    public roadNodes: Node[] = [];
    start() {

    }

    update(deltaTime: number) {
        if(this.roadNodes.length > 0 ){
            if(this.nextRoadNode == null){
                this.nextRoadNode = this.roadNodes[this.roadIndex]
            }
            let roadPosition = this.nextRoadNode.position
            if(Vec3.distance(this.node.position, roadPosition)<0.5){
                this.roadIndex++;
                this.roadIndex = this.roadIndex % this.roadNodes.length;
                this.nextRoadNode = this.roadNodes[this.roadIndex]
            }else{
                let dir = new Vec3();
                Vec3.subtract(dir,roadPosition, this.node.position);
                dir = dir.normalize();
                let move = dir.multiplyScalar(this.speed * deltaTime);
                let result = new Vec3();
                this.node.position = Vec3.add(result, this.node.position, move)
            }

        }

    }
}


